package com.jiang.starter.redis;

import com.jiang.starter.redis.lock.aop.LockAspect;
import com.jiang.starter.redis.lock.LockProperties;
import com.jiang.starter.redis.lock.JLock;
import com.jiang.starter.redis.lock.impl.RedissonJLock;
import org.redisson.api.RedissonClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * Redisson配置
 */
@Configuration
@Import(LockAspect.class)
@EnableConfigurationProperties({RedisProperties.class, LockProperties.class})
public class LockAutoConfiguration {

    /**
     * 注入分布式锁，业务框架有需要也可以自己实现
     */
    @Bean
    @ConditionalOnMissingBean(JLock.class)
    public JLock locker(LockProperties lockProperties, RedissonClient redissonClient) {
        return new RedissonJLock(lockProperties.getType(), redissonClient);
    }
}
